// pages/index/index.js
Page({
  data: {
    userList: [],
    loading: false,
    // 模拟数据 - 丰富的排行榜数据
    mockData: [
      {
        id: 1,
        nickname: '木木火日立',
        score: 3850,
        level: 'SSS级传奇',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-15'
      },
      {
        id: 2,
        nickname: '天才小神童',
        score: 3420,
        level: 'SS级大神',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-15'
      },
      {
        id: 3,
        nickname: '努力拼搏者',
        score: 3150,
        level: 'S级大师',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-14'
      },
      {
        id: 4,
        nickname: '刻苦钻研君',
        score: 2890,
        level: 'S级大师',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-14'
      },
      {
        id: 5,
        nickname: '勤奋好学生',
        score: 2650,
        level: 'A级专家',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-13'
      },
      {
        id: 6,
        nickname: '进步飞速侠',
        score: 2420,
        level: 'A级专家',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-13'
      },
      {
        id: 7,
        nickname: '坚持不懈君',
        score: 2180,
        level: 'A级专家',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-12'
      },
      {
        id: 8,
        nickname: '奋发向上者',
        score: 1950,
        level: 'B级高手',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-12'
      },
      {
        id: 9,
        nickname: '积极进取人',
        score: 1720,
        level: 'B级高手',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-11'
      },
      {
        id: 10,
        nickname: '稳步提升星',
        score: 1580,
        level: 'B级高手',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-11'
      },
      {
        id: 11,
        nickname: '新手练习生',
        score: 1320,
        level: 'C级新星',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-10'
      },
      {
        id: 12,
        nickname: '初学小萌新',
        score: 1150,
        level: 'C级新星',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-10'
      },
      {
        id: 13,
        nickname: '菜鸟也有梦',
        score: 980,
        level: 'C级新星',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-09'
      },
      {
        id: 14,
        nickname: '加油小战士',
        score: 820,
        level: 'D级入门',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-09'
      },
      {
        id: 15,
        nickname: '努力小蜗牛',
        score: 650,
        level: 'D级入门',
        avatar: '/static/images/default-avatar.png',
        updateTime: '2024-01-08'
      }
    ]
  },

  onLoad() {
    console.log('页面加载，开始加载用户数据')
    this.loadUserData()
  },

  onPullDownRefresh() {
    this.refreshData()
  },

  // 加载用户数据
  loadUserData() {
    console.log('loadUserData 开始执行')
    
    this.setData({ 
      loading: true,
      userList: [] // 先清空列表
    })
    
    // 调用云函数获取真实数据
    wx.cloud.callFunction({
      name: 'getUserList',
      success: res => {
        console.log('云函数调用成功:', res)
        if (res.result && res.result.success) {
          this.setData({
            userList: res.result.data || [],
            loading: false
          })
          console.log('真实数据加载完成，userList 长度:', res.result.data.length)
        } else {
          console.error('云函数返回错误:', res.result)
          this.loadMockData() // 云函数失败时使用假数据
        }
      },
      fail: err => {
        console.error('云函数调用失败:', err)
        wx.showToast({
          title: '网络错误，使用离线数据',
          icon: 'none'
        })
        this.loadMockData() // 云函数失败时使用假数据
      }
    })
  },

  // 加载假数据（作为后备方案）
  loadMockData() {
    console.log('使用假数据作为后备')
    this.setData({
      userList: this.data.mockData,
      loading: false
    })
    console.log('假数据设置完成，userList 长度:', this.data.mockData.length)
  },

  // 获取排名样式类
  getRankingClass(index) {
    if (index === 0) return 'rank-first'
    if (index === 1) return 'rank-second'  
    if (index === 2) return 'rank-third'
    return ''
  },

  // 获取排名图标
  getRankIcon(index) {
    const icons = ['🥇', '🥈', '🥉']
    return icons[index] || (index + 1)
  },

  // 跳转管理后台
  goToAdmin() {
    wx.navigateTo({
      url: '/pages/admin/admin'
    })
  },

  // 长按提示
  showAdminHint() {
    wx.showModal({
      title: '管理提示',
      content: '点击进入管理后台，需要输入管理员密码',
      showCancel: false
    })
  },

  // 刷新数据
  refreshData() {
    wx.showLoading({
      title: '刷新中...'
    })
    
    // 重新调用云函数获取最新数据
    wx.cloud.callFunction({
      name: 'getUserList',
      success: res => {
        wx.hideLoading()
        if (res.result && res.result.success) {
          this.setData({
            userList: res.result.data || []
          })
          wx.showToast({
            title: '刷新完成',
            icon: 'success'
          })
        } else {
          wx.showToast({
            title: '刷新失败',
            icon: 'none'
          })
        }
        if (wx.stopPullDownRefresh) {
          wx.stopPullDownRefresh()
        }
      },
      fail: err => {
        console.error('刷新失败:', err)
        wx.hideLoading()
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        })
        if (wx.stopPullDownRefresh) {
          wx.stopPullDownRefresh()
        }
      }
    })
  }
})
